package com.taiji.dianthus.controller.sys;

import com.alibaba.excel.metadata.Sheet;
import com.taiji.dianthus.common.BasicController;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.domain.sys.DSysUser;
import com.taiji.dianthus.domain.sys.LogExcel;
import com.taiji.dianthus.domain.sys.SysDictionary;
import com.taiji.dianthus.service.sys.DictionaryService;
import com.taiji.dianthus.service.sys.RoleUserService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.util.EasyExcelUtils;
import com.taiji.dianthus.util.IPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

import static com.taiji.dianthus.common.Constant.LOG_OPERATION_QUERY;

/**
 * @ClassName SysLogController
 * @Description 日志控制类
 * @Author H.M
 * @Date 2020/6/28
 */
@RequestMapping(value = "/sys/log")
@Controller
public class SysLogViewController extends BasicController {

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private DictionaryService dictionaryService;






    /**
     * 查询日志列表
     *
     * @param request
     * @param model
     * @param pageSize
     * @param currentPage
     * @param operation
     * @param startDate
     * @param endDate
     * @param userName
     * @param result
     * @param operationDesc
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request, Model model, Integer pageSize, Integer currentPage, Integer operation, Integer result,
                       String startDate, String endDate, String userName, String operationDesc) {
        pageSize = getPageSize(pageSize);
        currentPage = getCurrentPage(currentPage);
        DSysUser currentUser = currentUser();
        BusinessMessage message = sysLogService.list(pageSize, currentPage, userName, operation,
                operationDesc, result, startDate, endDate,currentUser);
        //查询字典表
        List<SysDictionary> operationDicts = dictionaryService.listByType("sysLogOperation");

        sysLogService.save(currentUser, LOG_OPERATION_QUERY, IPUtil.getIpAddr(request),
                "查询日志列表", message.getSuccess() ? 1 : 0, message.getMsg());
        StringBuffer url = new StringBuffer("/sys/log/list?pageSize=10");
        if (!StringUtils.isEmpty(userName)) {
            url.append("&userName=" + userName);
            model.addAttribute("userName", userName);
        }
        if (!StringUtils.isEmpty(operationDesc)) {
            url.append("&operationDesc=" + operationDesc);
            model.addAttribute("operationDesc", operationDesc);
        }
        if (null != operation && operation != 0) {
            url.append("&operation=" + operation);
            model.addAttribute("operation", operation);
        }

        if (null != result) {
            url.append("&result=" + result);
            model.addAttribute("result", result);
        }
        //开始时间
        if (!StringUtils.isEmpty(startDate)) {
            url.append("&startDate=" + startDate);
            model.addAttribute("startDate", startDate);
        }
        //结束时间
        if (!StringUtils.isEmpty(endDate)) {
            url.append("&endDate=" + endDate);
            model.addAttribute("endDate", endDate);
        }
        model.addAttribute("operationDicts", operationDicts);
        model.addAttribute("url", url);
        model.addAttribute("currentUser", currentUser);
        model.addAttribute("menus", listMenu());
        model.addAttribute("data", message.getData());
        return "pages/sys/sysLogList";
    }

    /**
     * 终端未用导出excel
     *
     * @return
     */
    @RequestMapping("/exportLog")
    public void exportNotUsedAgent(HttpServletResponse resp, Integer operation, Integer result,
                                   String startDate, String endDate, String userName, String operationDesc) {
        try {
            DSysUser currentUser = currentUser();
            if (!StringUtils.isEmpty(userName) && !"".equals(userName)) {
                userName = userName.trim();
            }
            if (!StringUtils.isEmpty(operationDesc) && !"".equals(operationDesc)) {
                operationDesc = operationDesc.trim();
            }
           List<LogExcel> list = sysLogService.listNoPage( userName, operation,
                    operationDesc, result, startDate, endDate,currentUser);
            Sheet sheet = new Sheet(1, 0, LogExcel.class);
            sheet.setSheetName("审计日志");
            EasyExcelUtils.writeExcelOneSheet(resp, list, sheet, "审计日志");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
